<?php
class omekpi_ctl_admin_deliverier extends desktop_controller{
    var $name = "发货绩效";
    var $workground = "performance";
    
    function index(){
        $deliverier_log = &$this->app->model('deliverier_log');
        $omekpi = $deliverier_log->getPageData();
        $count = $deliverier_log->getCount();
        $this->pagedata['begin_date'] = $_POST['begin_date'];
        $this->pagedata['end_date'] = $_POST['end_date'];
        $this->pagedata['count']=$count;
        $this->pagedata['deliveryList'] = $omekpi;
        $this->pagedata['csv_data'] = serialize($omekpi);
        $this->page('admin/performance/deliverier_log.html');
    }
    
   /* 
    //array('delivery_id'=>'1212','account_id'=>'1212');
    function deliveryIncremental($array = array()){
        $ser_array= array();
        $addarray = array();
        $time = date('Ymd');
        $invoice = &$this->app->model('deliverier_invoice');
        $deliverier_log = &$this->app->model('deliverier_log');
        $delivery = app::get('ome')->model('delivery');
        $employee = &$this->app->model('employee');
        $dcount = $delivery->count();
        $addarray['employee_id'] = $array['employee_id'];
        $darray['employee_id'] = $array['employee_id'];

        //$darray['deliveried_numbers'] = 0;//已发货量可从数库里查，查得到+1更新所有，查不到=1
        
        $deliveried_numbers=$deliverier_log->getDeliveried_numbers();
        $deliveried = 0;
        if($deliveried_numbers){
                $deliveried=$deliveried_numbers['deliveried_numbers']+1;
        }
        $del_filter['employee_id'] = $addarray['employee_id'];
        $del_filter['createdate'] = $time;
        $res = $deliverier_log->dump($del_filter);
        if($res){
            $addarray['createdate'] = $time;
            $addarray['my_deliveried_numbers'] = $res['my_deliveried_numbers']+1;
            $deliverier_log->update($addarray);
        }else{
            $darray['createdate'] = $time;
            $darray['delivery_numbers'] = 1000;
            $darray['deliveried_numbers'] = $deliveried;
            $darray['my_deliveried_numbers'] = 1;
            $deliverier_log->save($darray);
        }
        $deliverier_log->updateCount($dcount);
        $deliverier_log->updateDeliveried($deliveried);//更新已发货共计个数表
        $invocie_data = array('employee_id'=>$addarray['employee_id'],'delivery_id'=>$array['delivery_id'],'createdate'=>date('Ymd'));
        $invoice->save($invocie_data);//更新员工和发货单对应表
    } 
*/
    function getDelivery(){
//          $filter['process']='FALSE';
        $filter['parent_id'] = 0;
        $this->finder('ome_mdl_delivery',array(
            'title' => '需发货共计管理',
            'base_filter' => $filter,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
       ));
    }

    function getDeliveried(){
        $filter['process']='TRUE';
        $filter['parent_id'] = 0;
        $this->finder('ome_mdl_delivery',array(
            'title' => '已发货共计管理',
            'base_filter' => $filter,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
       ));
    }
    
    function getMydeliveried(){
//process true false 
        $employee_id = $_GET['id'];
        $createdate=date('Ymd',strtotime($_GET['date']));
        $servicer_invoice = $this->app->model('deliverier_invoice');
        $deliverier_ids =  $servicer_invoice->getList('delivery_id',array('employee_id'=>$employee_id,'createdate'=>$createdate),0,-1);

        $d_ids = array();
        if($deliverier_ids) {
            foreach($deliverier_ids as $v){
                array_push($d_ids, $v['delivery_id']);
            }
            $filter['process']='TRUE';
        }
        $filter['delivery_id']=$d_ids;
        $this->finder('ome_mdl_delivery',array(
            'title' => '我的发货单管理',
            'base_filter' => $filter,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
       ));

    }
    
    function getPagedata($sList=array(),$slog_List=array()){
        $omekpi=array();
        foreach($sList as $ke=>$va){//人员列表
            foreach($slog_List as $k=>$v){//绩效列表
                if($v['employee_id'] == $va['employee_id']){ 
                    $array['employee_id'] =$v['employee_id'];
                    $array['employee_name'] = $va['employee_name'];
                    $array['employee_op_no'] = $va['employee_op_no'];
                    $array['delivery_numbers'] = $v['delivery_numbers'];
                    $array['deliveried_numbers'] = $v['deliveried_numbers'];
                    $array['my_deliveried_numbers'] = $v['my_deliveried_numbers'];
                    $array['createdate'] = $v['createdate'];
                    array_push($omekpi,$array);
                    $array = null;
                }
            }
        }
        return $omekpi;
    }
    
//查询
    function search(){
        $this->begin('index.php?app=omekpi&ctl=admin_deliverier&act=index');
        $deliverier_log = &$this->app->model('deliverier_log');
        $employee = $this->app->model('employee');
        if (!$_POST['begin_date']&&!$_POST['end_date']&&!$_POST['have']){
            $this->end(true,'搜索完成');
        }

        $begin_date = date('Ymd',strtotime($_POST['begin_date']));
        $end_date = date('Ymd',strtotime($_POST['end_date']));
        if($_POST['begin_date'] && $_POST['end_date'] && ($begin_date > $end_date)){
            $this->end(false,'开始时间大于结束时间');
        }

        if (!$_POST['have']){//如果没有名字
            $where = 'where 1 ';
            if($_POST['begin_date']){
            $where .= "and createdate >= '".$begin_date."'";
            }
            if($_POST['end_date']){
                $where .= "and createdate <= '".$end_date."'";
            }
            $slogList = $deliverier_log->search($where);
            $em_filter = array('employee_type'=>'deliverier');
            $sList = $employee->getList('*',$em_filter,0,-1);
            $searchList = $this->getPagedata($sList,$slogList);
            if(!$searchList){
                $this->end(true,'无当前条件结果');
            }
            $count = $deliverier_log->getCount($where);
        }else{
            $filter=array();
            if($_POST['select']=='NA' ){
                $filter['employee_name'] = $_POST['have'];
            }else{
                if($_POST['select']=='NO' ){
                    $filter['employee_op_no'] = $_POST['have'];
                }
            }
            $filter['employee_type']='deliverier';
            $sList = $employee->getList('*',$filter,0,-1);
            $where = ' where 1 ';
            $in = '';
            if($sList){
                foreach($sList as $v){
                    $in .=$v['employee_id'].',';
                }
                $where .=' and employee_id in ('.substr($in,0,strlen($in)-1).')';
            }else{
                $this->end(false,$_POST['have'].'不在发货员列表中');
            }
            if($_POST['begin_date']){
                $where .= " and createdate >= '".$begin_date."'";
            }
            
            if($_POST['end_date']){
                $where .= " and createdate <= '".$end_date."'";
            }
            $slogList = $deliverier_log->search($where);
            $searchList = $this->getPagedata($sList,$slogList);
            if(!$searchList){
                $this->end(true,'无当前条件结果');
            }
            $count = $deliverier_log->getCount($where);
        }
        $this->pagedata['count']=$count;
        $this->pagedate['employee_op_no']=$_POST['employee_op_no'];
        $this->pagedata['employee_name']=$_POST['employee_name'];
        $this->pagedata['begin_date'] = $_POST['begin_date'];
        $this->pagedata['end_date'] = $_POST['end_date'];
        $this->pagedata['deliveryList'] = $searchList;
        $this->pagedata['csv_data'] = serialize($searchList);
        $this->page('admin/performance/deliverier_log.html');
    }

//导出
    function export(){
        $this->begin("index.php#app=omekpi&ctl=admin_performance_delivery&act=index");
        $res = unserialize(stripcslashes($_POST['csv_data']));
        $mess = '员工编号,员工姓名,需发货共计,已发货共计,我的发货数量,日期';
        if($res){
            foreach($res as $value){
                $mess .= "\r".$value['employee_op_no'].",".
                         $value['employee_name'].",".
                         $value['delivery_numbers'].",".
                         $value['deliveried_numbers'].",".
                         $value['my_deliveried_numbers'].",".
                         $value['createdate']."\r";
            }
        }
        header("Content-type: application/octet-stream");
        header("Accept-Ranges:bytes");
        $filename = '发货绩效'.date('m月d日',time()).".csv";
        $encoded_filename = urlencode($filename);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }
        //header("Content-Disposition: attachment; filename=".'发货绩效'.date('m月d日',time()).".csv");
        echo mb_convert_encoding($mess,"GBK","UTF-8");
    }

    
//文件后缀名
    function extName($file){
        return substr($file,strrpos($file,'.'));
    }


    
}
?>
